home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / FileOpen.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-26  |  10.1 KB  |  367 lines

  1. /* 
  2.  *  FileOpen.cpp 
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24.  
  25. #include "FlasKMPEG.h"
  26. #include "RunState.h"
  27. #include "error.h"
  28. #include ".\Input\DVDSelector.h"
  29. #include ".\FileOpen.h"
  30.  
  31. extern TConfig   o;
  32. extern TRunState rs;
  33. extern HWND hMainWnd;
  34. extern void EnableMenu();
  35. extern void DisableMenu();
  36. extern VBitmap                DecodedImage;
  37. extern unsigned char        MPEGImage[MAX_IMAGE_MEM];
  38. extern HINSTANCE hInst;
  39.  
  40. int OpenFile()
  41. {
  42.     OPENFILENAME oifn;
  43.     char    InputFile[1024];
  44.     CStreamDetector *detector;
  45.     int selected;
  46.  
  47.     // Initialize OPENFILENAME
  48.     ZeroMemory(&oifn, sizeof(OPENFILENAME));
  49.     oifn.lStructSize = sizeof(OPENFILENAME);
  50.     oifn.hwndOwner = hMainWnd;
  51.     strcpy(InputFile,"");
  52.     oifn.lpstrFile = InputFile;
  53.     oifn.nMaxFile = sizeof(InputFile);
  54.     oifn.lpstrFilter = "MPEG video files\0*.m2v;*.mpg;*.vob;*.m1v;*.m2v;*.mpe;*.mpeg;*.mv2;*.mpv\0All\0*.*\0";
  55.     oifn.nFilterIndex = 1;
  56.     oifn.lpstrFileTitle = "FlasK MPEG - Choose input stream file";
  57.     oifn.nMaxFileTitle = 0;
  58.     oifn.lpstrInitialDir = o.options.inputDir;
  59.     oifn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  60.     if(!GetOpenFileName(&oifn))
  61.         return 0;
  62.  
  63.     //Main_Bitstream_Filename = InputFile;
  64.     if(rs.audio){
  65.             delete rs.audio;
  66.             rs.audio=NULL;
  67.     }
  68.     if(rs.video){
  69.             delete rs.video;
  70.             rs.video=NULL;
  71.     }
  72.     if( rs.SD == NULL ){  //Stream Detector structure doesn't exist
  73.         rs.SD=new TStreamDetector;    //Create a new one
  74.     }
  75.     else{
  76.         delete rs.SD;
  77.         rs.SD=new TStreamDetector;
  78.     }
  79.     //Create a new stream detector
  80.     detector= new CStreamDetector;
  81.     //Set input file
  82.     if(detector)
  83.         if(!detector->SetFile(InputFile)){
  84.             PrintError(UNS_FILE_OPEN,(int)hMainWnd, 0);
  85.             delete detector;
  86.             return FALSE;
  87.         }
  88.  
  89.     //Now, recognize stream
  90.     switch(detector->RecognizeType())
  91.     {                    
  92.         case 0:
  93.             // Set the beginning of the compile
  94.             rs.startFilePos = 0;
  95.             //Stream was recognized as a valid stream
  96.             //Detect streams inside file
  97.             if(!detector->GetStreams(rs.SD, o.options.lurk_size )){                        //There was a problem detecting the streams
  98.                 PrintError(UNS_FILE_OPEN,(int)hMainWnd, 0);
  99.                 delete detector;
  100.                 return FALSE;
  101.             }
  102.  
  103.             //After recognizing type, you've obtained stream and substream
  104.             //available
  105.             if(rs.SD->n_video_streams){
  106.                 selected=rs.SD->video_selected;
  107.             if(rs.SD->video[selected].streamID) 
  108.                 rs.video = new VideoWrapper( InputFile, rs.SD->video[selected].streamID, -1, -1, -1, DVD_MODE);
  109.             }
  110.             else {
  111.                 PrintError(NO_VIDEO_TRACK,(int)hMainWnd, 0);
  112.                 return FALSE;
  113.             }
  114.  
  115.             if(rs.SD->n_audio_streams){
  116.                 selected=rs.SD->audio_selected[0];
  117.             if(rs.SD->audio[selected].streamID) 
  118.                 rs.audio = new Audio( InputFile, rs.SD->audio[selected].streamID, rs.SD->audio[selected].subStreamID, DVD_MODE);
  119.             }
  120.             else 
  121.                 rs.audio=NULL;
  122.  
  123. #ifdef TESTING_MULTIAUDIO
  124.                         rs.audio2 = new Audio( InputFile, 0xBD, 0x82, DVD_MODE);
  125. #endif
  126.  
  127.             //No error enable MENU
  128.             EnableMenu();
  129.  
  130.             switch(rs.video->detectedFrameRateCode){
  131.                         case 1:
  132.                             // HACK: LSX PLUGIN WANTS SCALE TO BE LIKE 2997/1000
  133.                             o.options.timeBase.scale=23976;
  134.                             o.options.timeBase.sampleSize=1000;
  135.                             break;
  136.                         case 2:
  137.                             o.options.timeBase.scale=24;
  138.                             o.options.timeBase.sampleSize=1;
  139.                             break;
  140.                         case 3:
  141.                             o.options.timeBase.scale=25;
  142.                             o.options.timeBase.sampleSize=1;
  143.                             break;
  144.                         case 4:
  145.                             o.options.timeBase.scale=2997;
  146.                             o.options.timeBase.sampleSize=100;
  147.                             break;
  148.                         case 5:
  149.                             o.options.timeBase.scale=30;
  150.                             o.options.timeBase.sampleSize=1;
  151.                             break;
  152.                         default:
  153.                             o.options.timeBase.scale=25;
  154.                             o.options.timeBase.sampleSize=1;
  155.                             break;
  156.                         }
  157.             
  158.  
  159.             //o.options.audioMode=DO_AUDIO;
  160.             if(rs.audio){
  161.                     /*if(audio->isMPEG && o.options.audioMode==DO_AUDIO)
  162.                         o.options.audioMode=NO_AUDIO;*/
  163.                 if( !rs.audio->isAC3  &&  !rs.audio->isMPEG )
  164.                         o.options.audioMode=NO_AUDIO;
  165.             }
  166.             else
  167.                 o.options.audioMode=NO_AUDIO;
  168.                         
  169.             return 0;
  170.             break;
  171.         case INPUT_NOEXIST:
  172.             MessageBox( hMainWnd, "Input file doesn't exit", "FlasK Warning", MB_OK);
  173.             break;
  174.         case IS_TRANSPORTSTREAM:
  175.             MessageBox( hMainWnd, "This file seems to be a transport stream. Transport Streams are not supported", "FlasK Warning", MB_OK);
  176.             break;
  177.         case NOT_RECOGNIZED:
  178.             MessageBox( hMainWnd, "This file doesn't seem to be a valid supported format. Supported formats are:\nMPEG2 program streams\nMPEG1 program streams\n", "FlasK Warning", MB_OK);
  179.             break;
  180.         case NO_AUDIOVIDEO:
  181.             MessageBox( hMainWnd, "This file doesn't seem to contain audio or video information.\n", "FlasK Warning", MB_OK);
  182.             break;
  183.         default:
  184.             break;
  185.     }
  186.     DisableMenu();
  187.     return 1;            
  188. }
  189.  
  190. int PrepareInputFromIFO(CinputStream *input, CIFOParser *ifo, TDVDSelector *sel)
  191. {
  192.     int i,val;
  193.     TPlaySequenceSpan inpSpan;
  194.  
  195.     if(!input || !ifo || !sel)
  196.         return 0;
  197.     for(i=0; i<ifo->PGCs[sel->selected_pgc].BuiltPGC[sel->selected_angle].playinfo.GetCount(); i++)
  198.     {
  199.         inpSpan.start  =      
  200.             ifo->PGCs[sel->selected_pgc].BuiltPGC[sel->selected_angle].playinfo[i].start;
  201.         inpSpan.end    =      
  202.             ifo->PGCs[sel->selected_pgc].BuiltPGC[sel->selected_angle].playinfo[i].end;
  203.  
  204.         val=input->AddSpan(&inpSpan);
  205.         if(!val)
  206.             return 0;
  207.     }
  208.     return input->ValidatePlaySequence();
  209. }
  210.  
  211. int OpenDVDFile()
  212. {
  213.     OPENFILENAME oifn;
  214.     char    InputFile[1024];
  215.  
  216.  
  217.     // Initialize OPENFILENAME
  218.     ZeroMemory(&oifn, sizeof(OPENFILENAME));
  219.     oifn.lStructSize = sizeof(OPENFILENAME);
  220.     oifn.hwndOwner = hMainWnd;
  221.     strcpy(InputFile,"");
  222.     oifn.lpstrFile = InputFile;
  223.     oifn.nMaxFile = sizeof(InputFile);
  224.     oifn.lpstrFilter = "DVD IFO files\0*.ifo\0All\0*.*\0";
  225.     oifn.nFilterIndex = 1;
  226.     oifn.lpstrFileTitle = "FlasK MPEG - Choose input DVD IFO file";
  227.     oifn.nMaxFileTitle = 0;
  228.     oifn.lpstrInitialDir = o.options.inputDir;
  229.     oifn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  230.     if(!GetOpenFileName(&oifn))
  231.         return IFO_OPENING_NOERROR;
  232.  
  233.     if(rs.audio){
  234.             delete rs.audio;
  235.             rs.audio=NULL;
  236.     }
  237.     if(rs.video){
  238.             delete rs.video;
  239.             rs.video=NULL;
  240.     }
  241.     if(rs.video_inp){
  242.             delete rs.video_inp;
  243.             rs.video_inp = NULL;
  244.     }
  245.     if(!(rs.video_inp = new CinputStream))
  246.         return 0;
  247.  
  248.     if(rs.audio_inp){
  249.             delete rs.audio_inp;
  250.             rs.audio_inp = NULL;
  251.     }
  252.     if(!(rs.audio_inp = new CinputStream))
  253.         return 0;
  254.  
  255.  
  256.     
  257.     
  258.  
  259.     DisableMenu();
  260.     rs.ifo = new CIFOParser;
  261.     int audio_stream_id,audio_substream_id, subpic_stream_id = -1, subpic_substream_id = -1;
  262.     int len;
  263.  
  264.     TDVDSelector sel_info;
  265.     if(rs.ifo->Load(InputFile))
  266.     {
  267.         if(OpenDVDSelector(hMainWnd, hInst, rs.ifo, &sel_info)){
  268.             
  269.             // Setting pgc selected into rs
  270.             rs.DVDSelections = sel_info;
  271.  
  272.             // Preparing input file
  273.             len = strlen(InputFile);
  274.  
  275.             InputFile[len-5] = '1';
  276.             InputFile[len-4] = '.';
  277.             InputFile[len-3] = 'v';
  278.             InputFile[len-2] = 'o';
  279.             InputFile[len-1] = 'b';
  280.             // Try opening the file
  281.             FILE *file;
  282.             if( !(file=fopen(InputFile, "rb")) )
  283.                 return 0;
  284.             fclose(file);
  285.  
  286.             rs.video_inp->OpenStream(InputFile, DVD_MODE);
  287.             rs.video_inp->SetWorkingMode(PLAYSEQ_MODE);
  288.             rs.audio_inp->OpenStream(InputFile, DVD_MODE);
  289.             rs.audio_inp->SetWorkingMode(PLAYSEQ_MODE);
  290.  
  291.             // Prepare CinputStream
  292.             if(!PrepareInputFromIFO(rs.video_inp, rs.ifo, &sel_info) ){
  293.                 delete rs.ifo;
  294.                 rs.ifo = NULL;
  295.                 return 0;
  296.             }
  297.             if(!PrepareInputFromIFO(rs.audio_inp, rs.ifo, &sel_info) ){
  298.                 delete rs.ifo;
  299.                 rs.ifo = NULL;
  300.                 return 0;
  301.             }
  302.  
  303.  
  304.             if(sel_info.has_subpic){
  305.               subpic_stream_id = 0xBD;
  306.               subpic_substream_id = 0x20 + sel_info.selected_subpic;
  307.             }
  308.  
  309.             rs.video = new VideoWrapper( InputFile, 0xE0, -1, subpic_stream_id, subpic_substream_id, DVD_MODE);
  310.             rs.video->UnSetInput();
  311.             rs.video->SetInput(rs.video_inp);
  312.  
  313.             if(sel_info.has_audio){
  314.                audio_stream_id = sel_info.audio_type == DVDSEL_AC3 ? 0xBD : (sel_info.audio_type==DVDSEL_MPEG ? (0xC0+sel_info.selected_audio) : 0 );
  315.             audio_substream_id = sel_info.audio_type == DVDSEL_AC3 ? (0x80+sel_info.selected_audio) : 0;
  316.                          rs.audio = new Audio( InputFile, audio_stream_id, audio_substream_id, DVD_MODE);
  317.                       rs.audio->UnSetInput();
  318.                       rs.audio->SetInput(rs.audio_inp);
  319.             }
  320.  
  321.  
  322.         }
  323.             // Set the beginning of the compile
  324.             rs.startFilePos = 0;
  325.     switch(rs.video->detectedFrameRateCode){
  326.                         case 1:
  327.                             // HACK: LSX PLUGIN WANTS SCALE TO BE LIKE 2997/1000
  328.                             o.options.timeBase.scale=23976;
  329.                             o.options.timeBase.sampleSize=1000;
  330.                             break;
  331.                         case 2:
  332.                             o.options.timeBase.scale=24;
  333.                             o.options.timeBase.sampleSize=1;
  334.                             break;
  335.                         case 3:
  336.                             o.options.timeBase.scale=25;
  337.                             o.options.timeBase.sampleSize=1;
  338.                             break;
  339.                         case 4:
  340.                             o.options.timeBase.scale=2997;
  341.                             o.options.timeBase.sampleSize=100;
  342.                             break;
  343.                         case 5:
  344.                             o.options.timeBase.scale=30;
  345.                             o.options.timeBase.sampleSize=1;
  346.                             break;
  347.                         default:
  348.                             o.options.timeBase.scale=25;
  349.                             o.options.timeBase.sampleSize=1;
  350.                             break;
  351.                         }
  352.             if(rs.audio){
  353.                     /*if(audio->isMPEG && o.options.audioMode==DO_AUDIO)
  354.                         o.options.audioMode=NO_AUDIO;*/
  355.                 if( !rs.audio->isAC3  &&  !rs.audio->isMPEG )
  356.                         o.options.audioMode=NO_AUDIO;
  357.             }
  358.             else
  359.                 o.options.audioMode=NO_AUDIO;
  360.     EnableMenu();
  361.     return IFO_OPENING_NOERROR;            
  362.     }
  363.     else{
  364.         DisableMenu();
  365.         return 0;
  366.     }
  367. }